Skip to content

Exit with non-zero code when database is not accessible#326

Merged
drwl merged 1 commit intomainfrom
re-raise-database-connection-errors
May 8, 2026
Merged

Exit with non-zero code when database is not accessible#326
drwl merged 1 commit intomainfrom
re-raise-database-connection-errors

Conversation

@OdenTakashi
Copy link
Copy Markdown
Collaborator

Problem

AnnotationDecider#annotate? has a catch-all rescue => e that returns false for any StandardError, including database connection errors. While error messages are printed to stderr, the command still exits with code 0 because the errors are treated as "skip this file" rather than a fatal condition. This is problematic for CI workflows using --frozen, where a non-zero exit code is expected on failure.

Solution

Add a specific rescue for ActiveRecord::ConnectionNotEstablished and ActiveRecord::NoDatabaseError before the catch-all handler. These errors now abort with a clear message and exit code 1, instead of being swallowed by the catch-all and treated as a skippable file-level issue. Other errors (e.g. model syntax errors, LoadError) continue to be rescued and skipped as before.

Follow-up

exe/annotaterb already has a # TODO: Return exit status comment, and Runner#run does not currently return a meaningful exit status.

As a follow-up, it may make sense to centralize error handling at the Runner or exe/annotaterb level. For example, the entire execution could be wrapped in a single top-level rescue that catches errors, prints a user-friendly message, and exits with a non-zero status. This would allow error handling to be managed in one place, rather than requiring specific exceptions to be handled in lower-level code such as AnnotationDecider.

ridgepole takes this approach.

However, that would move this change closer to the broader error-handling design, so it has been kept out of scope for this PR.

refs: #240

## Problem
AnnotationDecider#annotate? has a catch-all `rescue => e` that returns false for any StandardError, including database connection errors.
While error messages are printed to stderr, the command still exits with code 0 because the errors are treated as "skip this file" rather than a fatal condition.
This is problematic for CI workflows using `--frozen`, where a non-zero exit code is expected on failure.

## Solution
Add a specific rescue for ActiveRecord::ConnectionNotEstablished and ActiveRecord::NoDatabaseError before the catch-all handler.
These errors now abort with a clear message and exit code 1, instead of being swallowed by the catch-all and treated as a skippable file-level issue.
Other errors (e.g. model syntax errors, LoadError) continue to be rescued and skipped as before.

refs: #240
@OdenTakashi OdenTakashi force-pushed the re-raise-database-connection-errors branch from a3d0f38 to 3c4058b Compare April 12, 2026 07:19
@drwl drwl merged commit b7612f1 into main May 8, 2026
50 checks passed
@drwl drwl deleted the re-raise-database-connection-errors branch May 8, 2026 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants